package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysQuestionDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SurveyTreeSelect;
import com.ruoyi.system.domain.SysGeneralSurvey;
import com.ruoyi.system.service.ISysGeneralSurveyService;
import com.ruoyi.system.service.ISysQuestionDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 普查问卷问题Controller
 *
 * @author ruoyi
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/system/generalsurvey")
public class SysGeneralSurveyController extends BaseController {
    @Autowired
    private ISysGeneralSurveyService sysGeneralSurveyService;
    @Autowired
    private ISysQuestionDomainService domainService;

    /**
     * 查询普查问卷问题列表
     *
     * @return
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGeneralSurvey sysGeneralSurvey) {

        startPage();
        List<SysGeneralSurvey> list = sysGeneralSurveyService.selectSysGeneralSurveyList(sysGeneralSurvey);
        return getDataTable(list);
    }

    /**
     * 查询普查问卷问题列表
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:list')")
    @GetMapping("/list2")
    public AjaxResult list2(SysGeneralSurvey sysGeneralSurvey) {

        List<SysGeneralSurvey> list = sysGeneralSurveyService.selectSysGeneralSurveyListWithRecordsByCompanyId(sysGeneralSurvey);
        return AjaxResult.success(list);
    }

    // @PreAuthorize("@ss.hasPermi('system:survey:list')")
    @GetMapping("/status")
    public AjaxResult checkGeneralSurveyStatus(SysGeneralSurvey sysGeneralSurvey) {

        List<SysGeneralSurvey> list = sysGeneralSurveyService.selectSysGeneralSurveyListWithRecordsByCompanyId(sysGeneralSurvey);
        HashMap<Long, List<SysGeneralSurvey>> questions = new HashMap<>();
        for (SysGeneralSurvey question : list) {
            if(questions.containsKey(question.getDomain())){
                List<SysGeneralSurvey> tmp=questions.get(question.getDomain());
                tmp.add(tmp.size(),question);

            }else{
                List<SysGeneralSurvey> tmp=new ArrayList<>();
                tmp.add(tmp.size(),question);
                questions.put(question.getDomain(),tmp);
            }
        }
        List<SysQuestionDomain> domains = domainService.selectQuestionDomainList(new SysQuestionDomain());
        List<TreeSelect> domainTree = domainService.buildQuestionDomainTreeSelect(domains);

        return AjaxResult.success(buildSurveyTreeSelect(domainTree,questions));
    }

    private List<SurveyTreeSelect> buildSurveyTreeSelect(List<TreeSelect> domainTree, HashMap<Long, List<SysGeneralSurvey>> questions){
        List<SurveyTreeSelect> strees=new ArrayList<>();
        for(TreeSelect tree : domainTree){
            SurveyTreeSelect stree=new SurveyTreeSelect(tree);
            if(tree.getChildren()!=null &&tree.getChildren().size()>0){
                List<SurveyTreeSelect> children= buildSurveyTreeSelect( tree.getChildren(),  questions);
                stree.setChildren(children);
                strees.add(strees.size(),stree);
            }else{
                if(questions.containsKey(tree.getId())){
                    stree.setQuestions(questions.get(tree.getId()));
                    strees.add(strees.size(),stree);
                }
            }
        }
        return strees;
    }

    /**
     * 导出普查问卷问题列表
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:export')")
    @Log(title = "普查问卷问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGeneralSurvey sysGeneralSurvey) {
        List<SysGeneralSurvey> list = sysGeneralSurveyService.selectSysGeneralSurveyList(sysGeneralSurvey);

        ExcelUtil<SysGeneralSurvey> util = new ExcelUtil<SysGeneralSurvey>(SysGeneralSurvey.class);
        util.exportExcel(response, list, "普查问卷问题数据");
    }

    /**
     * 获取普查问卷问题详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId) {
        return AjaxResult.success(sysGeneralSurveyService.selectSysGeneralSurveyByQuestionId(questionId));
    }

    /**
     * 新增普查问卷问题
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:add')")
    @Log(title = "普查问卷问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGeneralSurvey sysGeneralSurvey) {
        return toAjax(sysGeneralSurveyService.insertSysGeneralSurvey(sysGeneralSurvey));
    }

    /**
     * 修改普查问卷问题
     */
    // @PreAuthorize("@ss.hasPermi('system:survey:edit')")
    @Log(title = "普查问卷问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGeneralSurvey sysGeneralSurvey) {
        return toAjax(sysGeneralSurveyService.updateSysGeneralSurvey(sysGeneralSurvey));
    }

    /**
     * 删除普查问卷问题
     */
    //  @PreAuthorize("@ss.hasPermi('system:survey:remove')")
    @Log(title = "普查问卷问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
        return toAjax(sysGeneralSurveyService.deleteSysGeneralSurveyByQuestionIds(questionIds));
    }
}
